package hujz.java.file_server;

import java.io.UnsupportedEncodingException;

/**
 * 文件信息
 */
public class FileMeta {

    private String id;
    private String uuid;
    private String fileName;
    private long size;
    private String owner;
    private String url;
    private String uri;
    private String sign;
    private String createTime;
    private String secret;
    private String from;
    private String password;
    private String remark;
    private String mediaType;
    private String status;
    private String ext;
    private String storageIdentify;

    public static class FileMetaFrom {
        private String deviceIp;
        private String deviceNum;
        private String deviceTyp;
        private String networkType;
        private String remoteIp;
        private String localPath;

        public String getLocalPath() {
            return localPath == null ? "" : localPath;
        }

        public void setLocalPath(String localPath) {
            this.localPath = localPath;
        }

        public String getDeviceIp() {
            return deviceIp == null ? "" : deviceIp;
        }

        public void setDeviceIp(String deviceIp) {
            this.deviceIp = deviceIp;
        }

        public String getDeviceNum() {
            return deviceNum == null ? "" : deviceNum;
        }

        public void setDeviceNum(String deviceNum) {
            this.deviceNum = deviceNum;
        }

        public String getDeviceTyp() {
            return deviceTyp == null ? "" : deviceTyp;
        }

        public void setDeviceTyp(String deviceTyp) {
            this.deviceTyp = deviceTyp;
        }

        public String getNetworkType() {
            return networkType == null ? "" : networkType;
        }

        public void setNetworkType(String networkType) {
            this.networkType = networkType;
        }

        public String getRemoteIp() {
            return remoteIp == null ? "" : remoteIp;
        }

        public void setRemoteIp(String remoteIp) {
            this.remoteIp = remoteIp;
        }

        public String toString() {
            StringBuilder bs = new StringBuilder();
            bs.append("deviceIp=").append(getDeviceIp().replaceAll("\n", "")).append("\n");
            bs.append("deviceNum=").append(getDeviceNum().replaceAll("\n", "")).append("\n");
            bs.append("deviceType=").append(getDeviceTyp().replaceAll("\n", "")).append("\n");
            bs.append("networkType=").append(getNetworkType().replaceAll("\n", "")).append("\n");
            bs.append("remoteIp=").append(getRemoteIp().replaceAll("\n", "")).append("\n");
            bs.append("localPath=").append(getLocalPath().replaceAll("\n", "")).append("\n");
            return bs.toString();
        }
    }

    public String getStorageIdentify() {
        return storageIdentify;
    }

    public void setStorageIdentify(String storageIdentify) {
        this.storageIdentify = storageIdentify;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getMediaType() {
        return mediaType;
    }

    public void setMediaType(String mediaType) {
        this.mediaType = mediaType;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    @Override
    public String toString() {
        return "FileMeta{" +
                "id='" + id + '\'' +
                ", uuid='" + uuid + '\'' +
                ", fileName='" + fileName + '\'' +
                ", size=" + size +
                ", owner='" + owner + '\'' +
                ", url='" + url + '\'' +
                ", sign='" + sign + '\'' +
                ", createTime='" + createTime + '\'' +
                ", secret='" + secret + '\'' +
                ", from='" + from + '\'' +
                ", password='" + password + '\'' +
                ", remark='" + remark + '\'' +
                ", mediaType='" + mediaType + '\'' +
                ", status='" + status + '\'' +
                '}';
    }

    public static byte[] serialize(FileMeta fileMeta) {
        StringBuilder sb = new StringBuilder();
        sb.append("0");
        if (fileMeta.getId() != null)
            sb.append("\nid=\"").append(fileMeta.getId().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getUuid() != null)
            sb.append("\nuuid=\"").append(fileMeta.getUuid().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getFileName() != null)
            sb.append("\nfileName=\"").append(fileMeta.getFileName().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        sb.append("\nsize=").append(fileMeta.getSize());
        if (fileMeta.getOwner() != null)
            sb.append("\nowner=\"").append(fileMeta.getOwner().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getUri() != null)
            sb.append("\nuri=\"").append(fileMeta.getUri().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getSign() != null)
            sb.append("\nsign=\"").append(fileMeta.getSign().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getCreateTime() != null)
            sb.append("\ncreateTime=\"").append(fileMeta.getCreateTime().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getSecret() != null)
            sb.append("\nsecret=\"").append(fileMeta.getSecret().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getFrom() != null)
            sb.append("\nfrom=\"").append(fileMeta.getFrom().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getPassword() != null)
            sb.append("\npassword=\"").append(fileMeta.getPassword().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getRemark() != null)
            sb.append("\nremark=\"").append(fileMeta.getRemark().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getMediaType() != null)
            sb.append("\nmediaType=\"").append(fileMeta.getMediaType().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getStatus() != null)
            sb.append("\nstatus=\"").append(fileMeta.getStatus().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getExt() != null)
            sb.append("\next=\"").append(fileMeta.getExt().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        if (fileMeta.getStorageIdentify() != null)
            sb.append("\nstorageIdentify=\"").append(fileMeta.getStorageIdentify().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"")).append("\"");
        sb.append("\n");
        try {
            return sb.toString().getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            // never
            return new byte[0];
        }
    }

    public static FileMeta deserialize(byte[] data) {
        String str = "";
        try {
            str = new String(data, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            // never
        }
        char c;
        StringBuilder line = new StringBuilder();
        boolean isQuote = false, isEscape = false;
        FileMeta fileMeta = new FileMeta();
        for (int i = 0; i < str.length(); i++) {
            c = str.charAt(i);
            if (isEscape) {
                line.append(c);
                isEscape = false;
            } else if (isQuote) {
                if (c == '"') {
                    isQuote = false;
                } else if (c == '\\') {
                    isEscape = true;
                } else {
                    line.append(c);
                }
            } else if (c == '\n') {
                // peek
                int n = line.indexOf("=");
                if (n != -1) {
                    String name = line.substring(0, n);
                    String value = line.substring(n + 1);
                    if (name.equals("id")) fileMeta.setId(value);
                    else if (name.equals("uuid")) fileMeta.setUuid(value);
                    else if (name.equals("fileName")) fileMeta.setFileName(value);
                    else if (name.equals("size")) fileMeta.setSize(Long.parseLong(value));
                    else if (name.equals("owner")) fileMeta.setOwner(value);
                    else if (name.equals("uri")) fileMeta.setUri(value);
                    else if (name.equals("sign")) fileMeta.setSign(value);
                    else if (name.equals("createTime")) fileMeta.setCreateTime(value);
                    else if (name.equals("secret")) fileMeta.setSecret(value);
                    else if (name.equals("from")) fileMeta.setFrom(value);
                    else if (name.equals("password")) fileMeta.setPassword(value);
                    else if (name.equals("remark")) fileMeta.setRemark(value);
                    else if (name.equals("mediaType")) fileMeta.setMediaType(value);
                    else if (name.equals("status")) fileMeta.setStatus(value);
                    else if (name.equals("ext")) fileMeta.setExt(value);
                    else if (name.equals("storageIdentify")) fileMeta.setStorageIdentify(value);
                }
                line.delete(0, line.length());
            } else if (c == '\\') {
                isEscape = true;
            } else if (c == '"') {
                isQuote = true;
            } else {
                line.append(c);
            }
        }
        return fileMeta;
    }
}
